<?php
include '../Module/TemplateModule.php';
$p=isset($_REQUEST["p"])?$_REQUEST["p"]:1;
$n=100;
$q=isset($_REQUEST["q"])?$_REQUEST["q"]:"";
$itemModule=new TemplateModule();
?>

<div class="mybutton icon disable"><div><img src="../images/icon/left1.png"/><span>Back</span></div></div>
<div class="mybutton icon spaceright" onclick="loadhtml('../Controller/?AUnit=item',$('#content_items'))"><div><img src="../images/icon/arrow_refresh1.png"/><span>Reload</span></div></div>
<div class="mybutton icon disable"><div><img src="../images/icon/media_floppy_green.png"/><span>Save</span></div></div>
<div class="mybutton icon" onclick="loaditemdetail(null);"><div><img src="../images/icon/addg.png"/><span>Add</span></div></div>

<div style="clear: both"></div>
 <table style="width:100%;" class="id-table-manage dt">
    <tr>
        <th class="_tal">ID</th>
        <th class="_tal">Image</th>
        <th class="_tal">Name</th>
        <th class="_tal">Category</th>
		<th class="_tal">Organization</th>
        <th class="_tal">Insert</th>
        <th class="_tal">Update</th>
        <th class="_tal">#</th>
    </tr>
    <?
    $dt=$itemModule->getTemplates_all($p,$n,$q,$_type,"");
    $numrow=$itemModule->getTemplates_NumRow_all($q, $_type,"");
    if($dt==null){
        echo "<tr><td colspan=8>No Data to display.</td></tr>";

    }else foreach ($dt as $row){
    ?>
    <tr class="id-row-head-<?=$row["id"]?>" >
        <td><?=$row["id"]?></td>
        <td onclick="loaditemdetail('<?=$row["id"]?>');">
            <img width="60px" height="40px" src="<?=$row["img"]?>"/>
                 
        </td>
        <td onclick="loaditemdetail('<?=$row["id"]?>');"><?=$row["name"]?></td>
        <td onclick="loaditemdetail('<?=$row["id"]?>');"><?=$row["categoryname"]?></td>
	<td onclick="loaditemdetail('<?=$row["id"]?>');"><?=$row["organizationname"]?></td>
        <td><?=$row["insert"]?></td>
        <td><?=$row["update"]?></td>
        <td class="">
            <?
            if(isset($row["delete"])){
            ?>

            <span style="color:red">Deleted at <?=$row["delete"]?></span>
            <div class="icon16 hover50" title="Restore This Item" onclick="restoreItem('<?=$row["id"]?>');">
                    <img src="../images/icon/gtk_undelete.png"/>
            </div>
            <?
            }
            ?>
            <span class="action">
                <?
                if(!isset($row["delete"])){
                ?>
                <div class="icon16 hover50" title="Delete" onclick="deleteItem('<?=$row["id"]?>');">
                    
                    <img src="../images/icon/16/JO_23_icon_delete_on.png"/>
                </div>
                <?
                }
                ?>
            </span>
        </td>
    </tr>
    
    <?
    }
    ?>
</table>
<div style="clear: both"></div>
<div class="mybutton icon disable"><div><img src="../images/icon/left1.png"/><span>Back</span></div></div>
<div class="mybutton icon spaceright" onclick="loadhtml('../Controller/?AUnit=item',$('#content_items'))"><div><img src="../images/icon/arrow_refresh1.png"/><span>Reload</span></div></div>
<div class="mybutton icon disable"><div><img src="../images/icon/media_floppy_green.png"/><span>Save</span></div></div>
<div class="mybutton icon" onclick="loaditemdetail(null);"><div><img src="../images/icon/addg.png"/><span>Add</span></div></div>
<div style="clear: both"></div>
<div class="box-menu hover50">
    <div>
    
    <div class="icon32 hover50" onclick="loadhtml('../Controller/?AUnit=item',null);" title="Refresh">
        <img class="" src="../images/icon/refresh1.png"/>
    </div>
    <div class="icon32 hover50" onclick="newitem()" title="Go to New Item">
        <img class="" src="../images/icon/add2.png"/>
    </div>
    </div>
</div>
<script>
    function restoreItem(id){
        if(!confirm("Do you restore this Item?")) return;
        var surl="../Controller/";
        var sdata={
                "controller":"TemplateController",
                "action":"restore",
                "_type":"<?=$_type?>",
                "id":id
        };
        jQuery.ajax({
                type:"POST", 
                data:sdata, 
                dataType:"text", 
                url:surl, 
                success: function (data){
                    if(data=="true"){
                        showmsg("Item have been Restore.",2000);
                        
                        var str="<span class=\"action\">";
                        str+="      <div class=\"icon16 hover50\" title=\"Delete\" onclick=\"deleteItem('"+id+"');\">";
                        str+="          <img src=\"../images/icon/remove.png\"/>";
                        str+="      </div>";
                        str+="   </span>";
                        $(".id-row-head-"+id).find("td:last-child").html(str);
                    }else{
                        showmsg(data,5000);
                    }

                },
                error: function (xhr, ajaxOptions, thrownError){
                        showmsg("<b>Status</b>:"+xhr.status+"<br/><b>ThrownError</b>:"+thrownError,5000);
                        
                }
        });
    }
    function deleteItem(id){
        if(!confirm("Do you delete this Item?")) return;
        var surl="../Controller/";
        var sdata={
                "controller":"TemplateController",
                "action":"delete",
                "_type":"<?=$_type?>",
                "id":id
        };
        jQuery.ajax({
                type:"POST", 
                data:sdata, 
                dataType:"text", 
                url:surl, 
                success: function (data){
                    if(data=="true"){
                        showmsg("Item have been Deleted.",2000);
                        var str="<span style=\"color:red\">Item have been Deleted</pan>";
                        str+="   <div class=\"icon16 hover50\" title=\"Restore This Item\" onclick=\"restoreItem('"+id+"');\">";
                        str+="      <img src=\"../images/icon/gtk_undelete.png\"/>";
                        str+="   </div>";
                        $(".id-row-head-"+id).find("td:last-child").html(str);
                        
                    }else{
                        showmsg(data,5000);
                    }

                },
                error: function (xhr, ajaxOptions, thrownError){
                        showmsg("<b>Status</b>:"+xhr.status+"<br/><b>ThrownError</b>:"+thrownError,5000);
                        
                }
        });
    }
</script>